#include "BinaryTree.h"

TreeNode* createTree() {
    // TreeNode* A = createNode(1);
    // TreeNode* B = createNode(2);
    // TreeNode* C = createNode(3);
    // TreeNode* D = createNode(4);
    // TreeNode* E = createNode(5);
    // TreeNode* F = createNode(6);
    // TreeNode* G = createNode(7);
    // TreeNode* H = createNode(8);
    // TreeNode* I = createNode(9);
    // TreeNode* J = createNode(10);
    // TreeNode* K = createNode(11);
    // TreeNode* L = createNode(12);
    // A->left = B;
    // A->right = C;
    // B->left = D;
    // B->right = E;
    // C->left = F;
    // C->right = G;
    // D->left = H;
    // D->right = I;
    // E->left = J;
    // F->right = K;
    // G->left = L;

    TreeNode* H = createNode(8);
    TreeNode* I = createNode(9);
    TreeNode* J = createNode(10);
    TreeNode* K = createNode(11);
    TreeNode* L = createNode(12);
    TreeNode* D = createNodeWithChildren(4, H, I);
    TreeNode* E = createNodeWithChildren(5, J, NULL);
    TreeNode* F = createNodeWithChildren(6, NULL, K);
    TreeNode* G = createNodeWithChildren(7, L, NULL);
    TreeNode* B = createNodeWithChildren(2, D, E);
    TreeNode* C = createNodeWithChildren(3, F, G);
    TreeNode* A = createNodeWithChildren(1, B, C);
    return A;
}

void test1() {
    TreeNode* root = createTree();
    printf("preOrder    :");
    show(preOrder, root);
    printf("preOrderNor :");
    show(preOrderNor, root);
    printf("inOrder     :");
    show(inOrder, root);
    printf("inOrderNor  :");
    show(inOrderNor, root);
    printf("postOrder   :");
    show(postOrder, root);
    printf("postOrderNor:");
    show(postOrderNor, root);
    printf("levelOrder  :");
    show(levelOrder, root);
    freeTree(root);
    root = NULL;
}

void test2() {
    TreeNode* root = createTree();
    printf("height: %d\n", height(root));
    printf("count: %d\n", count(root));
    printf("countOfKLevel, k = 0:  %d\n", countOfKLevel(root, 0));
    printf("countOfKLevel, k = 1:  %d\n", countOfKLevel(root, 1));
    printf("countOfKLevel, k = 2:  %d\n", countOfKLevel(root, 2));
    printf("countOfKLevel, k = 3:  %d\n", countOfKLevel(root, 3));
    printf("countOfKLevel, k = 4:  %d\n", countOfKLevel(root, 4));
    printf("countOfKLevel, k = 10: %d\n", countOfKLevel(root, 10));
    printf("get B: %d\n", get(root->left));
    freeTree(root);
    root = NULL;
}

void test3() {
    TreeNode* root = createTree();
    ElemType* arr;
    int size = 0;
    arr = (ElemType*)malloc(sizeof(ElemType) * count(root));
    preOrderToArray(root, arr, &size);
    printf("preOrderToArray: [ ");
    for (int i = 0; i < size; ++i) {
        printf("%d ", arr[i]);
    }
    printf("]\n");
    free(arr);
}

int main() {
    void (*test[])() = {test1, test2, test3};
    int len = sizeof(test) / sizeof(*test);
    for (int i = 0; i < len; ++i) {
        printf("test %d==========================\n", i + 1);
        test[i]();
        printf("\n");
    }
}